package com.danicsoft.daide.serverclient;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.danicsoft.daide.binary.Connection;
import com.danicsoft.daide.command.OrderSpec;
import com.danicsoft.daide.command.Turn;
import com.danicsoft.daide.command.UnitPos;
import com.danicsoft.daide.token.StandardToken;
import com.danicsoft.daide.token.TokenMap.Phases;

/**
 * Implements a bot that can hold its position and remove any units that need to be removed
 * @author Daniel Yule (daniel.yule@gmail.com)
 * @version 1.0
 *
 */
public class HoldBot implements Player {

	/**
	 * The passcode for this game;
	 */
	private int passcode;

	/**
	 * The power we are playing.
	 */
	private StandardToken power;

	/**
	 * The server we are connected to.
	 */
	private Server myServer;

	/**
	 * Create a new Holdbot and connect to the server.
	 * 
	 * @param server
	 *        The server to connect to.
	 */
	public HoldBot(Server server) {
		myServer = server;
		myServer.join(this);
	}
	
	/**
	 * Run a holdbot that connects to the localhost on the default port.
	 * @param args
	 */
	public static void main(String [] args) {
		try {
			ServerProxy server = new ServerProxy(InetAddress.getLocalHost(), Connection.DEFAULT_PORT);
			new HoldBot(server);
		}
		catch (UnknownHostException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void accept() {

	}

	public void acceptOrder(OrderSpec order, byte note) {

	}

	public void adminMessage(String from, String message) {

	}

	public String getName() {

		return "Kaylee Holdbot";
	}

	public int getPasscode() {
		return passcode;
	}

	public StandardToken getPower() {
		return power;
	}

	public PLAYER_STATE getState() {
		return null;
	}

	public String getVersion() {
		return "0.1";
	}

	public void nextTurn(TurnInfo turn) {
		submitOrders(turn);

	}

	public void notifyMapInfo(MapInfo mInfo) {
		myServer.ready(this);
	}

	public void notifyOfMissingOrders(List<UnitPos> missingPositions) {

	}

	public void notifyOfMissingRemovals(int removalAmount) {

	}

	public void notifyOfMissingRetreats(List<UnitPos> missingRetreats) {

	}

	public void notifyUpcomingDeadline(int secondsLeft) {

	}

	public void reject() {

	}

	public void startGame(StandardToken playerPower, int playerPasscode, VariantInfo info, TurnInfo startingInfo) {
		power = playerPower;
		passcode = playerPasscode;
		submitOrders(startingInfo);
	}

	/**
	 * Submit the holding orders to the server.
	 * @param curTurn
	 */
	private void submitOrders(TurnInfo curTurn) {
		List<OrderSpec> orders = new ArrayList<OrderSpec>();
		switch (curTurn.turn.season.getSubType()) {
			case Phases.SPR:
			case Phases.FAL:
				for (UnitPos position : curTurn.armyPosition.positions()) {
					if (position.power.equals(power)) {
						orders.add(OrderSpec.getHoldOrder(position));
					}
				}
				break;
			case Phases.SUM:
			case Phases.AUT:
				for (UnitPos position : curTurn.armyPosition.positions()) {
					if (position.power.equals(power) && position.isRetreating()) {
						orders.add(OrderSpec.getDisbandOrder(position));
					}
				}
		}
		try {
			if(orders.size() > 0)
				myServer.submitOrders(orders, this);
		}
		catch (IllegalServerStateException e) {
			e.printStackTrace();
		}

	}

	public void startGame(TurnInfo startingTurn) {
	// TODO Automatically generated

	}

	public void endGame(List<StandardToken> winners, Turn turn) {
		//TODO Automatically generated
		
	}

}
